﻿/*******************************************************************************
* 
*Author         :  VN
*Description    :  处理广播包帮助类
*Other          :  none
*Modify Record  :  none
*
*******************************************************************************/
using System.Collections.Generic;

namespace Network
{
    public class Broadcast
    {
        Dictionary<uint, CMsgDelegate> m_MsgBrocastMap = new Dictionary<uint, CMsgDelegate>();      // 广播包队列
/*        private LuaFunction OnSocket = null;*/

        public Broadcast()
        {
            //OnSocket = LuaClient.GetMainState().GetFunction("NetworkMgr.OnSocket");
        }

        // 监听广播消息
        public void AddBroadcastEventHander(uint cmdID, CMsgDelegate callback)
        {
            if (callback == null)
            {
                return;
            }

            if (m_MsgBrocastMap.ContainsKey(cmdID))
            {
                m_MsgBrocastMap[cmdID] -= callback;
                m_MsgBrocastMap[cmdID] += callback;
            }
            else
            {
                m_MsgBrocastMap[cmdID] = callback;
            }

            if (RunMode.Instance.LogNetwork)
            {
                string strLog = "AddBroadcastEventHander" + cmdID.ToString() + "callback=" + callback.ToString();
                LuaInterface.Debugger.Log(strLog);
            }
        }

        public void ClearAll()
        {
            if (m_MsgBrocastMap != null)
            {
                m_MsgBrocastMap.Clear();
            }
        }

        // 移除广播消息
        public void RemoveBroadcastEventHander(uint cmdID, CMsgDelegate callback)
        {
            if (callback == null)
            {
                return;
            }

            string strLog = "";
            if (!m_MsgBrocastMap.ContainsKey(cmdID))
            {
                if (RunMode.Instance.LogNetwork)
                {
                    strLog = "RemoveBroadcastEventHander null" + cmdID.ToString() + "callback=" + callback.ToString();
                    LuaInterface.Debugger.Log(strLog);
                }
                return;
            }
            
            m_MsgBrocastMap[cmdID] -= callback;
            if (m_MsgBrocastMap[cmdID] == null)
            {
                m_MsgBrocastMap.Remove(cmdID);
            }

            if (RunMode.Instance.LogNetwork)
            {
                strLog = "RemoveBroadcastEventHander" + cmdID.ToString() + "callback=" + callback.ToString();
                LuaInterface.Debugger.Log(strLog);
            }
        }

        // 广播消息回调處理
        public void DispatchMsg(CmdPack cmdPack)
        {
            
        }
    }
}